#!/bin/bash
################################################################################
# Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The OpenAirInterface Software Alliance licenses this file to You under
# the Apache License, Version 2.0  (the "License"); you may not use this file
# except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#-------------------------------------------------------------------------------
# For more information about the OpenAirInterface (OAI) Software Alliance:
#      contact@openairinterface.org
################################################################################
# file build_hss
# brief
# author Lionel Gauthier
# company Eurecom
# email: lionel.gauthier@eurecom.fr
#
################################
# include helper functions
################################
THIS_SCRIPT_PATH=$(dirname $(readlink -f $0))
source $THIS_SCRIPT_PATH/../build/tools/build_helper



function help()
{
  echo_error " "
  echo_error "Usage: build_hss [OPTION]..."
  echo_error "Build the experimental HSS executable. (oai_hss)"
  echo_error " "
  echo_error "Options:"
  echo_error "Mandatory arguments to long options are mandatory for short options too."
  echo_error "  -c, --clean                               Clean the build generated files: config, object, executable files (build from scratch)"
  echo_error "  -d, --debug                               Compile with debug informations."
  echo_error "  -D, --daemon                              Build HSS as a daemon."
  echo_error "  -F, --force                               No interactive script for installation of software packages."
  echo_error "  -h, --help                                Print this help."
  echo_error "  -i, --check-installed-software            Check installed software packages necessary to build and run HSS (support $SUPPORTED_DISTRO)."
  echo_error "                                            If filled, then OPc key in table users for all subscribers (IMSI) will be computed/updated."
  echo_error "  -v, --verbose                             Build process verbose."
}

NAME=oai_hssd
DAEMON=/usr/sbin/$NAME
DAEMON_ARGS=""
PIDFILE=/var/run/$NAME.pid


function main()
{
  local -i clean=0
  local -i daemon=0
  local -i verbose=0
  local -i var_check_install_oai_software=0
  local -i force=0
  local    cmake_args=" "
  local    make_args="-j`nproc`"
  local    realm=""

  until [ -z "$1" ]; do
    case "$1" in
      -c | --clean)
        clean=1
        echo "Clean the build generated files (build from scratch)"
        shift;
        ;;
      -d | --debug)
        cmake_args="$cmake_args -DDEBUG=1"
        echo "Compile with debug informations"
        shift;
        ;;
      -D | --daemon)
        daemon=1
        cmake_args="$cmake_args -DDAEMONIZE=1"
        echo "Build HSS as a daemon"
        shift;
        ;;
      -F | --force)
        force=1
        echo "Force set (no interactive)"
        shift;
        ;;
      -h | --help)
        help
        exit 0
        ;;
      -i | --check-installed-software)
        echo "Check installed software packages necessary to build and run HSS (support $SUPPORTED_DISTRO):"
        set_openair_env
        var_check_install_oai_software=1
        shift;
        ;;
      -v | --verbose)
        echo "Make build process verbose"
        cmake_args="$cmake_args -DCMAKE_VERBOSE_MAKEFILE=ON"
        make_args="VERBOSE=1 $make_args"
        verbose=1
        shift;
        ;;
      *)
        echo "Unknown option $1"
        help
        exit 1
        ;;
    esac
  done

  if [ ! -d /usr/local/etc/oai ]; then
    $SUDO mkdir -p /usr/local/etc/oai/freeDiameter
  fi

  if [ $var_check_install_oai_software -gt 0 ];then
    update_package_db
    check_install_hss_software  $force
    echo "HSS not compiled, to compile it, re-run build_hss without -i option"
    exit 0
  fi


  set_openair_env
  if [[ $verbose -eq 1 ]]; then
    cecho "OPENAIRCN_DIR    = $OPENAIRCN_DIR" $green
  fi

  if [[ z$OPENAIRCN_DIR = z ]]; then
    echo_fatal "OPENAIRCN_DIR env variable not set, exiting"
  fi


  local dlog=$OPENAIRCN_DIR/build/log
  local dconf=OPENAIRCN_DIR/oai_hss

  mkdir -m 777 -p $dlog $dlog


  cmake_args="$cmake_args -DOPENAIRCN_DIR=$OPENAIRCN_DIR"

  cd $OPENAIRCN_DIR/build/hss
  if [ $clean -ne 0 ]; then
    if [[ $verbose -eq 1 ]]; then
      echo "Cleaning HSS"
    fi
    rm -Rf build 2>&1
    mkdir -m 777 -p -v build
  fi
  if [ ! -d ./build ]; then
    mkdir -m 777 -p -v build
  fi
  cd ./build
  $CMAKE $cmake_args .. > /dev/null
  make $make_args
  chmod -f 777 *
  if [ $daemon -ne 0 ]; then
    do_stop_daemon
    $SUDO cp -upv $OPENAIRCN_DIR/build/hss/build/oai_hss /usr/sbin/oai_hssd && echo_success "oai_hssd installed"
  else
    $SUDO killall -q oai_hss
    $SUDO cp -upv $OPENAIRCN_DIR/build/hss/build/oai_hss /usr/local/bin/ && echo_success "oai_hss installed"
  fi
}


main "$@"


